﻿using System;
using Microsoft.Practices.Unity;

namespace TestByWire.Factories.StubbingStrategies
{
    internal class StubCreationStrategyFactory : IStubCreationStrategyFactory
    {
        private readonly IStubCreationStrategy[] _creationStrategies;

        internal StubCreationStrategyFactory(IUnityContainer container)
        {
            _creationStrategies = new IStubCreationStrategy[]
            {
                new StubbingStrategy(container),
                new ActivatorStrategy(),
                new StringStubbingStrategy(),
                new ValueTypeAndStringArrayStrategy()
            };
        }

        public IStubCreationStrategy DetermineStrategy(Type type, string instanceName)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            foreach (var stubCreationStrategy in _creationStrategies)
            {
                if (stubCreationStrategy.CanHandleType(type, instanceName))
                {
                    return stubCreationStrategy;
                }
            }

            throw new InvalidOperationException(@"No strategy could be found which is suitable for creating a stub for type " + type.Name);
        }
    }
}
